/*
 * @Author: Miao 747296174@qq.com
 * @Date: 2023-07-24 17:15:07
 * @LastEditors: Miao 747296174@qq.com
 * @LastEditTime: 2023-07-24 17:17:33
 * @FilePath: /ipc/IpcShmCli.cc
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include "Comm.hpp"
#include "Log.hpp"
#include <cstdio>
#include <unistd.h>
using namespace std;
// 充当使用共享内存的角色
int main()
{
    int fd = Open(FIFO_FILE, WRITER);
    // 创建相同的key值
    key_t key = CreateKey();
    Log() << "key: " << key << "\n";
    // 获取共享内存
    int shmid = shmget(key, MEM_SIZE, IPC_CREAT);
    if (shmid < 0)
    {
        Log() << "shmget: " << strerror(errno) << "\n";
        return 2;
    }
    // 挂接
    char *str = (char*)shmat(shmid, nullptr, 0);
    sleep(5);
    // 用它
    // 竟然没有使用任何的系统调用接口！
    while(true)
    {
        printf("Please Enter# ");
        fflush(stdout);
        ssize_t s = read(0, str, MEM_SIZE);
        cout <<  s << endl;
        if(s > 0)
        {
            str[s] = '\0';
        }
        Signal(fd);
    }
  
    // 去关联
    shmdt(str);

    return 0;
}
